package com.zhou.springsecurityoauth2.service.impl;

import cn.hutool.core.io.LineHandler;
import cn.hutool.core.lang.copier.Copier;
import com.alibaba.fastjson.annotation.JSONField;
import com.zhou.springsecurityoauth2.entity.User;
import com.zhou.springsecurityoauth2.mapper.UserMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhou
 * @version 1.0
 * @date 2022/10/8 - 10 - 08 - 11:24
 * @description com.zhou.springsecurityoauth2.service.impl
 */
@Service
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {

    private final UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User byUserNameUser = userMapper.findByUserNameUser(username);
        if (Objects.isNull(byUserNameUser)){
            throw new UsernameNotFoundException("用户名密码错误");
        }
        ArrayList<String> strings = new ArrayList<>();
        return new LoginUser(byUserNameUser,strings);

    }

    @Data
    @NoArgsConstructor
    public class LoginUser implements UserDetails{
        private User user;
        //存储权限信息
        private List<String> permissions;

        public LoginUser(User user, List<String> permissions) {
            this.user = user;
            this.permissions = permissions;
        }

        //存储SpringSecurity所需要的权限信息的集合
        @JSONField(serialize = false)
        private List<GrantedAuthority> authorities;

        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            if(authorities!=null){
                return authorities;
            }
            return permissions.stream().map(a->new SimpleGrantedAuthority(a)).collect(Collectors.toList());
        }

        @Override
        public String getPassword() {
            return user.getPassword();
        }

        @Override
        public String getUsername() {
            return user.getUserName();
        }

        @Override
        public boolean isAccountNonExpired() {
            return true;
        }

        @Override
        public boolean isAccountNonLocked() {
            return true;
        }

        @Override
        public boolean isCredentialsNonExpired() {
            return true;
        }

        @Override
        public boolean isEnabled() {
            return true;
        }
    }
}



